package com.aurora.oa.mycenter.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.listener.OnRequestIsSuccessListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 修改姓名，性别，等基本信息
 */
public class ChangeUserInfoActivity extends BaseActivity {
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.ll_common_input)
    LinearLayout llCommonInput;
    @Bind(R.id.rb_male)
    RadioButton rbMale;
    @Bind(R.id.rb_female)
    RadioButton rbFemale;
    @Bind(R.id.rg_sex)
    RadioGroup rgSex;
    @Bind(R.id.ll_change_sex)
    LinearLayout llChangeSex;
    private String title;
    private String param_key;
    //标题右侧选项
    private TextView tv_right_title;
    private String value;
    RequestHttpModel model = new RequestHttpModelImpl();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = getIntent().getStringExtra("title");
        param_key = getIntent().getStringExtra("update_key");
        value = getIntent().getStringExtra("value");
        initTitlebar(true, title, true);
        setMyContentView(R.layout.activity_change_item_userinfo);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tv_right_title = (TextView) findViewById(R.id.tv_right_title);
        tv_right_title.setText("保存");
        tv_right_title.setVisibility(View.VISIBLE);
        tv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (param_key){
                    case AuroraConstants.UserInfoKey.SEX:
                        if(rbMale.isChecked()){
                            value = "男";
                        }else{
                            value = "女";
                        }
                        break;
                    default:
                        value = etContent.getText().toString();
                        break;
                }
                //上传服务器，修改个人信息
                Map<String,String> param = new HashMap<String, String>();
                param.put(param_key,value);
                model.doUpdateUserInfo(param, new OnRequestIsSuccessListener() {
                    @Override
                    public void onRequestSuccess(String info) {
                        ToastUtil.show(getApplicationContext(),"修改成功");
                        finish();
                    }

                    @Override
                    public void onRequestFail(String message) {
                        ToastUtil.show(getApplicationContext(),message);
                    }
                });
            }
        });
        switch (param_key){
            case AuroraConstants.UserInfoKey.SEX:
                llChangeSex.setVisibility(View.VISIBLE);
                if(value.equals("女")){
                    rbFemale.setChecked(true);
                }else{
                    rbMale.setChecked(true);
                }
                break;
            default:
                llCommonInput.setVisibility(View.VISIBLE);
                etContent.setText(value);
                break;
        }

    }
}
